<template>
  <!--待分配案件管理-->
  <section class="page system-parameter-manage">
    <data-form :model="queryModel" @handleResetForm="follFeedback" @onSearch="refreshData" :page="pageService" autoSearch>
      <!--输入区域-->
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="queryModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="queryModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="queryModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="queryModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="机构" prop="outSourceType">
          <!-- <el-select v-model="queryModel.outSourceType" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OutSourceType')" :key="index" :label="name" :value="code"></el-option>
          </el-select> -->
          <select-department :specialOptions="specialOptions" v-model="queryModel.outSourceType"></select-department>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-cascader v-model="queryModel.area" :options="$city.getCityData()" change-on-select clearable></el-cascader>
        </el-form-item>
        <!-- <el-form-item label="顾客类型" prop="customerCategory">
          <el-select v-model="queryModel.customerCategory" placeholder="所有" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CustomerCategory')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="案件金额" prop="outAmount">
          <number-range v-model="queryModel.outAmount">
          </number-range>
        </el-form-item>
        <el-form-item label="逾期阶段" prop="overduePeriod">
          <el-select v-model="queryModel.overduePeriod" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="queryModel.overdueDays">
          </number-range>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="queryModel.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="queryModel.productName"></el-input>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker type="daterange" :editable="false" v-model="queryModel.followInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
      </template>
    </data-form>
    <data-box @onPageChange="refreshData" :data="stayDataList" :selectionList.sync="selectionList" :page="pageService">
      <template slot="buttons">
        <el-button @click="division" v-auth = "1012">分案</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="area" label="地区" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $city.getInheritNames(Number(row.area))" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="案件金额" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :formatter="(row) => $filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <!-- <el-table-column prop="customerCategory" label="顾客类型" :formatter="(row) => $filter.dictConvert(row.customerCategory,'CustomerCategory')" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column> -->
        <el-table-column prop="turnSource" label="流转来源" :formatter="(row) => $filter.dictConvert(row.turnSource,'TurnSource')" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :min-width="$helper.getColumnWidth(5)">
          <template slot-scope="scope">
            {{scope.row.followInTime|dateTimeFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="followupBack" label="行动代码" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictcustomConvert(row.followupBack)">
        </el-table-column>
        <el-table-column prop="lawerStatus" label="诉讼状态" :formatter="(row) => $filter.dictConvert(row.lawerStatus,'LawerStatus')" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="unCheatResult" label="反欺诈结果" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
      </template>
    </data-box>
    <!--弹框-分案-->
    <el-dialog title="选择委外方" @close="dialog.selectEntrusted = false" :visible.sync="dialog.selectEntrusted" :close-on-click-modal="false" :show-close="false">
      <select-entrusted :isLawsuit="false" :selectedCaseIds="selectedCaseIds" @close="dialog.selectEntrusted=false;refreshData()" @success="refreshData"></select-entrusted>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataForm from '~/components/common/data-form.vue';
import DataBox from '~/components/common/data-box.vue';
import NumberRange from '~/components/common/number-range.vue';
import SelectEntrusted from '~/components/outsource-manage/case-allot/select-entrusted.vue'; // 选择委外方
import { Mutation, Getter, namespace, State } from 'vuex-class';
import { PageService } from '~/utils/page.service';
import { SystemParamService } from '~/services/management-service/system-param.service';
import { CityService } from '~/utils/city.service';
import { FilterService } from '~/utils/filter.service';
import { OutsourceStayService } from '~/services/domain-service/outsource-stay-manage.service';
import { SortService } from '~/utils/sort.service';
import { OpenExecutionData } from "~/config/types";
import { ApplyType, CASE_CHANNEL, CASE_HALT_CATEGORY, } from '~/config/enum.config';
import SelectDepartment from "~/components/business-component/select-department.vue";
import { CommonService } from "~/utils/common.service";
import { OutsourceService } from "~/services/domain-service/outsource.service";

const CollectionExecutionModule = namespace('collection-execution');

// @Auth(1002)
@Layout('workspace')
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    SelectEntrusted,
    SelectDepartment
  },
})
export default class StayManage extends Vue {
  @Dependencies(SystemParamService)
  private sysParamService: SystemParamService;
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(OutsourceStayService)
  private outsourceStayService: OutsourceStayService;
  @Dependencies(OutsourceService) private outsourceService: OutsourceService;
  @CollectionExecutionModule.Action private openCollectionExecution;

  private dialog: any = {
    selectEntrusted: false,
    caseDistribution: false,
  };
  private queryModel: any = {
    batchNumber: '',
    caseNumber: '',
    personalName: '',
    area: [],
    mobileNo: '',
    idCard: '',
    localAddress: '',
    currentCollector: '',
    overduePeriod: '',
    turnSource: '',
    sysCode: '',
    productName: '',
    followInTime: [],
    followupBack: '',
    outAmount: {
      max: '',
      min: '',
    },
    overdueDays: {
      max: '',
      min: '',
    },
  };
  private stayDataList: any[] = []; //数据列表
  private selectionList: any[] = []; //选中数据
  private caseDistributionDataList: any = {}; //选中数据
  private temp: any = {}; //选中数据
  private tempData: any = [];
  private specialOptions:any[] = []


  private mounted() {
    this.refreshData();
    this.getOuterSource();
  }
  private division() {
    if (this.selectionList.length === 0) {
      this.$message.info('请选择待分配案件')
      return
    }
    this.dialog.selectEntrusted = true
  }

  //获取委外部门不包括律所
  private getOuterSource() {
    this.outsourceService.getOuterSource().subscribe(
      data =>{ this.specialOptions = CommonService.generateTreeData(data, {
        keyName: 'id',
        parentKeyName: 'parentId'
      })
      // data =>{console.log(data,"data")
    })
  }

  private refreshData() {
    this.selectionList = []
    // 默认加载列表
    this.outsourceStayService
      .searchPendingList(this.queryModel, this.pageService)
      .subscribe(
        (data) => {
          this.stayDataList = data;
          if(this.stayDataList){
          this.stayDataList.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
        }
      );
  }


  private get selectedCaseIds() {
    return this.selectionList.map(v => v.id)
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(new OpenExecutionData(row.id, row.caseNumber, CASE_CHANNEL.OUTER, true,true))
  }

  // 重置numberRange
  private follFeedback() {
    this.queryModel.outAmount = { min: '', max: '' };
    this.queryModel.overdueDays = { min: '', max: '' };
  }
}
</script>

<style lang="less" >
</style>
